#include "sharpen.h"
using namespace std;

Sharpen::Sharpen()
{  k_rows=5;
    k_cols=5;
    int i;
    kernel=new double[k_rows*k_cols];
    kernel[0]=-1.0;
    kernel[1]=-1.0;
    kernel[2]=-1.0;
    kernel[3]=-1.0;
    kernel[4]=-1.0;
    kernel[5]=-1.0;
    kernel[6]=-1.0;
    kernel[7]=-1.0;
    kernel[8]=-1.0;
    kernel[9]=-1.0;
    kernel[10]=-1.0;
    kernel[11]=-1.0;
    kernel[12]=-1.0;
    kernel[13]=25;
    kernel[14]=-1.0;
    kernel[15]=-1.0;
    kernel[16]=-1.0;
    kernel[17]=-1.0;
    kernel[18]=-1.0;
    kernel[19]=-1.0;
    kernel[20]=-1.0;
    kernel[21]=-1.0;
    kernel[22]=-1.0;
    kernel[23]=-1.0;
    kernel[24]=-1.0;


}
Sharpen::~Sharpen(){
    delete [] kernel;
}

QImage Sharpen::normalize(int *R,int *G,int *B){
    QImage qout=QImage(in_image->width(),in_image->height(),in_image->format());
    int rows=in_image->height();
    int cols=in_image->width();
    int i,j,max=0,min=255;
    int maxR=0,maxG=0,maxB=0,minR=255,minG=255,minB=255;
    for(i=0;i<cols;i++)
        for(j=0;j<rows;j++){

        if(R[i+j*cols]>maxR)
            maxR=R[i+j*cols];
        if(G[i+j*cols]>maxG)
            maxG=G[i+j*cols];
        if(B[i+j*cols]>maxB)
            maxB=B[i+j*cols];
        if(R[i+j*cols]<minR)
            minR=R[i+j*cols];
        if(G[i+j*cols]<minG)
            minG=G[i+j*cols];
        if(B[i+j*cols]<minB)
            minB=B[i+j*cols];
            }

    int sizeR=(minR>0)?maxR+minR+1:maxR-minR+1;
    int sizeG=(minG>0)?maxG+minG+1:maxG-minG+1;
    int sizeB=(minB>0)?maxB+minB+1:maxB-minB+1;

    int *histogramR=new int[sizeR];
    int *histogramG=new int[sizeG];
    int *histogramB=new int[sizeB];
    for(i=0;i<sizeR;i++)
    {
        histogramR[i]=0;

    }
    for(i=0;i<sizeG;i++)
    {
        histogramG[i]=0;

    }
    for(i=0;i<sizeB;i++)
    {
        histogramB[i]=0;

    }
    double mR,mG,mB;
    double cR,cG,cB;
    mR=(sizeR-1)*1.0/(maxR-minR);
    cR=-mR*minR;
    mG=(sizeG-1)*1.0/(maxG-minG);
    cG=-mG*minG;
    mB=(sizeB-1)*1.0/(maxB-minB);
    cB=-mB*minB;
    cout<<maxR<<" "<<minR<<" "<<maxG<<" "<<minG<<" "<<maxB<<" "<<minB<<endl;
    for(int x=0;x<cols;x++)
        for(int y=0;y<rows;y++)
        {
            //cout<<(int)(R[x+y*cols]*mR+cR)<<endl;
            histogramR[(int)(R[x+y*cols]*mR+cR)]++;
            histogramG[(int)(G[x+y*cols]*mG+cG)]++;
            histogramB[(int)(B[x+y*cols]*mB+cB)]++;
        }
    int limit=rows*cols*0.000375;
    cout<<"limit"<<limit<<endl;
    int sum=0;

    for(i=0;i<sizeR;i++)
    {
        //cout<<i<<" "<<histogramR[i]<<endl;
        sum=sum+histogramR[sizeR-i-1];
        if(sum>limit){
            maxR=(sizeR-i-1-cR)/mR;
            break;
        }

    }
    sum=0;
    for(i=0;i<sizeR;i++)
    {
        //cout<<i<<" "<<histogramR[i]<<endl;
        sum=sum+histogramR[i];
        if(sum>limit){
            minR=(i-cR)/mR;
            break;
        }

    }
    sum=0;
    for(i=0;i<sizeG;i++)
    {
        //cout<<i<<" "<<histogramR[i]<<endl;
        sum=sum+histogramG[sizeG-i-1];
        if(sum>limit){
            maxG=(sizeG-i-1-cG)/mG;
            break;
        }

    }
    sum=0;
    for(i=0;i<sizeG;i++)
    {
        //cout<<i<<" "<<histogramR[i]<<endl;
        sum=sum+histogramG[i];
        if(sum>limit){
            minG=(i-cG)/mG;
            break;
        }

    }
    sum=0;
    for(i=0;i<sizeB;i++)
    {
        //cout<<i<<" "<<histogramR[i]<<endl;
        sum=sum+histogramB[sizeB-i-1];
        if(sum>limit){
            maxB=(sizeB-i-1-cB)/mB;
            break;
        }

    }
    sum=0;
    for(i=0;i<sizeB;i++)
    {
        //cout<<i<<" "<<histogramR[i]<<endl;
        sum=sum+histogramB[i];
        if(sum>limit){
            minB=(i-cB)/mB;
            break;
        }

    }
    if(maxR>maxG){
        if(maxR>maxB)
            max=maxR;
        else
            max=maxB;
    }
    else {if(maxG>maxB)
        max=maxG;
        else
                max=maxB;
    }
    if(minR<minG){
        if(minR<minB)
            min=minR;
        else
            min=minB;}
    else {if(minG<minB)
        min=minG;
        else
                min=minB;
    }


    cout<<maxR<<" "<<minR<<" "<<maxG<<" "<<minG<<" "<<maxB<<" "<<minB<<endl;

    double m=255.0/(max-min);
    mR=255.0/(maxR-minR);
    mG=255.0/(maxG-minG);
    mB=255.0/(maxB-minB);
    double c=-m*min;
    cR=-mR*minR;
    cG=-mG*minG;
    cB=-mB*minB;
    cout<<"m is"<<m<<"max is"<<max<<"min is"<<min<<endl;
    for(i=0;i<cols;i++)
        for(j=0;j<rows;j++){
        qout.setPixel(i,j,qRgb((m*R[i+j*cols]+c+qRed(in_image->pixel(i,j)))/2,(m*G[i+j*cols]+c+qGreen(in_image->pixel(i,j)))/2,(m*B[i+j*cols]+c+qBlue(in_image->pixel(i,j)))/2));
    }
    cout<<"here"<<endl;
    delete [] R;
    delete [] G;
    delete [] B;
    return qout;
    };



//QImage Sharpen::normalize(int *R,int *G,int *B)
